<?php
/**
 * ApplicationConfigBehavior is a behavior for the application.
 * It loads additional config parameters that cannot be statically
 * written in config/main
 */
class ModuleBehavior extends CBehavior {
	/**
	 * Declares events and the event handler methods
	 * See yii documentation on behavior
	 */
	public function events() {
		return array_merge ( parent::events (), array (
				//'onBeginRequest' => beginRequest 
				'onBeginRequest' => beginRequest
		) );
	}
	
	/**
	 * 自动载入表数据中的modules,不用写在config/main.php中
	 * 等同于main.php中的
	 * array(
	 * ...
	 * 'modules'=>array('modulename'=>array(...))
	 * ...
	 * );
	 *
	 * Load configuration that cannot be put in config/main
	 */
	public function beginRequest() {
		$modules = array ();
		// 报错 Property "CDbCriteria.isactive" is not defined.
		// $m = new LoadModule();
		// 过滤掉默认字段属性值
		// $m->unsetAttributes();
		$model = LoadModule::model ()->findAll (); // Todo – should be per
		// $sql = 'SELECT * FROM ' . LoadModule::model ()->tableName () . ' WHERE isactive=1';
		// $row = Yii::app ()->db->createCommand ( $sql )->queryAll ();
		if ($model == false)
			return false;
		foreach ( $model as $item ) {
			// if (! empty ( $item ['value'] ))
			$modules [$item ->modulename] = array (); // Todo can set
		}
		Yii::app ()->setModules ( $modules );
	}
}

?>